<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>GSAP布局切换动画</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/Flip.min.js"></script>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      background: #f5f5f5;
    }
    
    .container {
      max-width: 1000px;
      margin: 0 auto;
      background: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }
    
    h1 {
      text-align: center;
      margin-bottom: 20px;
      color: #333;
    }
    
    .controls {
      display: flex;
      justify-content: center;
      gap: 10px;
      margin-bottom: 20px;
    }
    
    button {
      padding: 10px 20px;
      background: #4a90e2;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
      transition: background 0.3s;
    }
    
    button:hover {
      background: #3a7bc8;
    }
    
    .layout-container {
      position: relative;
      height: 500px;
      margin-top: 20px;
      background: #f9f9f9;
      border-radius: 8px;
      overflow: hidden;
    }
    
    .cards-container {
      display: grid;
      gap: 20px;
      padding: 20px;
      height: 100%;
    }
    
    /* 布局A - 网格布局 */
    .cards-container.layout-a {
      grid-template-columns: repeat(2, 1fr);
      grid-template-rows: repeat(2, 1fr);
    }
    
    /* 布局B - 垂直列表布局 */
    .cards-container.layout-b {
      grid-template-columns: 1fr;
      grid-template-rows: repeat(4, 1fr);
    }
    
    .card {
      background: #4a90e2;
      color: white;
      border-radius: 8px;
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      transition: background 0.3s;
    }
    
    .card:hover {
      background: #3a7bc8;
    }
    
    .explanation {
      margin-top: 30px;
      padding: 20px;
      background: #f0f8ff;
      border-radius: 8px;
      line-height: 1.6;
    }
    
    .explanation h2 {
      margin-bottom: 10px;
      color: #333;
    }
    
    .explanation p {
      margin-bottom: 15px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>GSAP布局切换动画</h1>
    
    <div class="controls">
      <button id="toggleFlip">使用Flip插件</button>
    </div>
    
    <div class="layout-container">
      <div class="cards-container layout-a">
        <div class="card">卡片1</div>
        <div class="card">卡片2</div>
        <div class="card">卡片3</div>
        <div class="card">卡片4</div>
      </div>
    </div>
    
    <div class="explanation">
      <h2>实现说明</h2>
      <p>这个示例展示了两种使用GSAP实现布局切换的方法：</p>
      <p><strong>2. Flip插件：</strong>使用GSAP的Flip插件自动处理布局变化，代码更简洁。</p>
      <p>点击上面的按钮可以体验不同的切换效果。</p>
    </div>
  </div>

  <script>
    // 确保在代码最前面注册Flip插件
    gsap.registerPlugin(Flip);
    
    // 使用Flip插件的实现
    document.getElementById('toggleFlip').addEventListener('click', function() {
      const container = document.querySelector('.cards-container');
      const currentLayout = container.classList.contains('layout-a') ? 'a' : 'b';
      const newLayout = currentLayout === 'a' ? 'b' : 'a';
      
      // 获取当前状态
      const state = Flip.getState('.card', { 
        props: "color,backgroundColor", 
        simple: true 
      });
      
      // 切换布局类
      container.classList.remove(`layout-${currentLayout}`);
      container.classList.add(`layout-${newLayout}`);
      
      // 执行动画
      Flip.from(state, {
        duration: 0.8,
        ease: "power1.inOut",
        stagger: 0.1,
        absolute: true,
        onComplete: () => {
          console.log("布局切换完成");
        }
      });
    });
  </script>
</body>
</html>
